package com.xmut.bs.handaccount.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xmut.bs.handaccount.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

public abstract class BaseFragment extends Fragment {

    private Unbinder unbinder;

    protected FragmentActivity mContext;
    private View view;
    private boolean isFirst;
    public int colorPrimary;

    /**
     * 字体库
     */
    public Typeface iconfont;

    /**
     * 获取布局ID
     */
    protected abstract int getContentViewLayoutID();

    /**
     * 界面初始化
     */
    protected abstract void init();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("BaseFragment", "onCreateView");
        if (view == null)
            if (getContentViewLayoutID() != 0) {
                view = inflater.inflate(getContentViewLayoutID(), container, false);
            } else {
                view = super.onCreateView(inflater, container, savedInstanceState);
            }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        mContext = getActivity();
        colorPrimary = getResources().getColor(R.color.colorPrimary);
        if (isShapeIcon()) {
            if (getActivity() != null && getActivity() instanceof BaseActivity && ((BaseActivity) getActivity()).isShapeIcon()) {
                iconfont = ((BaseActivity) getActivity()).iconfont;
            } else {
                iconfont = Typeface.createFromAsset(getActivity().getAssets(), "iconfont.ttf");
            }
        }
        return view;
    }

    /**
     * 是否使用字体图片
     */
    protected boolean isShapeIcon() {
        return false;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.i("BaseFragment", "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        if (!isFirst) {
            init();
            isFirst = true;
        }
    }

    @Override
    public void onDestroyView() {
        Log.i("BaseFragment", "onDestroyView");
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i("BaseFragment", "onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        Log.i("BaseFragment", "onAttach");
        super.onAttach(context);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.i("BaseFragment", "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.i("BaseFragment", "onActivityCreated");
        super.onStart();
    }

    @NonNull
    @Override
    public View getView() {
        return view;
    }

    /**
     * 带参数的页面跳转
     *
     * @param clz    跳转Activity
     * @param bundle 参数
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mContext, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivity(Intent intent, Class<?> clz) {
        intent.setClass(mContext, clz);
        startActivity(intent);
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    public void startActivityForResult(Class<?> cls, Intent intent,
                                       int requestCode) {
        intent.setClass(mContext, cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 带返回值的页面跳转
     *
     * @param cls         跳转Activity
     * @param bundle      参数
     * @param requestCode 请求码
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, new Intent(), requestCode);
    }

    private Toast mToast;

    protected void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext.getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    public void initRefreshLayout(BGARefreshLayout refreshLayout, BGARefreshLayout.BGARefreshLayoutDelegate delegate) {
        // 为BGARefreshLayout 设置代理
        refreshLayout.setDelegate(delegate);
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
        //        BGARefreshViewHolder refreshViewHolder = new XXXImplRefreshViewHolder(this, true);
        // 设置下拉刷新和上拉加载更多的风格
        BGANormalRefreshViewHolder refreshViewHolder = new BGANormalRefreshViewHolder(mContext, true);
        refreshLayout.setRefreshViewHolder(refreshViewHolder);

        // 为了增加下拉刷新头部和加载更多的通用性，提供了以下可选配置选项  -------------START
        // 设置正在加载更多时不显示加载更多控件
        refreshLayout.setIsShowLoadingMoreView(false);
        // 设置正在加载更多时的文本
        //        refreshViewHolder.setLoadingMoreText("加载更多");
        // 设置整个加载更多控件的背景颜色资源 id
        //        refreshViewHolder.setLoadMoreBackgroundColorRes(loadMoreBackgroundColorRes);
        // 设置整个加载更多控件的背景 drawable 资源 id
        //        refreshViewHolder.setLoadMoreBackgroundDrawableRes(loadMoreBackgroundDrawableRes);
        // 设置下拉刷新控件的背景颜色资源 id
        //        refreshViewHolder.setRefreshViewBackgroundColorRes(refreshViewBackgroundColorRes);
        // 设置下拉刷新控件的背景 drawable 资源 id
        //        refreshViewHolder.setRefreshViewBackgroundDrawableRes(refreshViewBackgroundDrawableRes);
        // 设置自定义头部视图（也可以不用设置）     参数1：自定义头部视图（例如广告位）， 参数2：上拉加载更多是否可用
        //        refreshLayout.setCustomHeaderView(mBanner, false);
        // 可选配置  -------------END
    }
}